package com.filldream.sun.bean.result;


import java.util.Collections;

/**
 * 对象controller层向页面返回的信息
 *
 * @author fishlord and RickSun
 * 2019-03-22 15:56
 */
public class ResultEntity<T> extends BaseResultEntity{

	/** 返回对象  */
	private T data;

	private Boolean checkOk;

	/**
	 * 空参构造器
	 */
	public ResultEntity(){}

	/**
	 * 全参构造器
	 * @param code
	 * @param msg
	 * @param data
	 */
	public ResultEntity(int code,String msg,String enmsg,T data){
		this.code=code;
		this.msg=msg;
		this.enmsg =enmsg;
		this.data = data;
		this.checkOk = null;
	}

	public ResultEntity(int code,String msg,String enmsg,T data,Boolean checkOk){
		this.code=code;
		this.msg=msg;
		this.enmsg =enmsg;
		this.data = data;
		this.checkOk = checkOk;
	}

	/**
	 * 默认返回错误码
	 * @param errorCode
	 * @return
	 */
	public static ResultEntity createError(ErrorCode errorCode){
		return new ResultEntity(errorCode.getCode(),errorCode.getMsg(),errorCode.getEnmsg(),Collections.EMPTY_LIST);
	}


	/**
	 * 默认成功
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T>ResultEntity<T> successd(T data){
		ErrorCode errorCode = ErrorCode.SUCCESS;
		if(data == null){
			errorCode = ErrorCode.EMPTY_RESULT;
			return new ResultEntity(errorCode.getCode(),errorCode.getMsg(),errorCode.getEnmsg(),Collections.EMPTY_MAP,false);
		}
		return new ResultEntity<T>(errorCode.getCode(),errorCode.getMsg(),errorCode.getEnmsg(),data);
	}

	/**
	 * 默认成功
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T>ResultEntity successd(ErrorCode errorCode,T data){
		return new ResultEntity<T>(errorCode.getCode(),errorCode.getMsg(),errorCode.getEnmsg(),data);
	}

	/**
	 * 默认失败
	 * @return
	 */
	public static ResultEntity fail(){
		ErrorCode errorCode = ErrorCode.FAIL;
		return new ResultEntity(errorCode.getCode(),errorCode.getMsg(),errorCode.getEnmsg(),Collections.EMPTY_MAP);
	}

	/**
	 * 默认失败
	 * @return
	 */
	public static ResultEntity fail(String msg){
		ErrorCode errorCode = ErrorCode.FAIL;
		return new ResultEntity(errorCode.getCode(),msg,errorCode.getEnmsg(),Collections.EMPTY_MAP);
	}


	public static <T>ResultEntity<T> autoSuccess(T data){
		if(data == null){
			return ResultEntity.fail();
		}
		ErrorCode errorCode = ErrorCode.SUCCESS;
		return new ResultEntity<T>(errorCode.getCode(),errorCode.getMsg(),errorCode.getEnmsg(),data);
	}


	public static <T>ResultEntity<T> diy(ErrorCode errorCode,String msg){
		return new ResultEntity(errorCode.getCode(),msg,errorCode.getEnmsg(),Collections.EMPTY_MAP);
	}


	/**
	 * 默认为空
	 * @return
	 */
	public static ResultEntity empty(){
		ErrorCode errorCode = ErrorCode.SUCCESS;
		return new ResultEntity(errorCode.getCode(),"empty","empty", Collections.EMPTY_MAP,false);
	}

	@Override
	public boolean isOk(){
		return this.code == 0 && (this.checkOk == null || this.checkOk);
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
}